<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="关键字" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入关键字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
        >
          <el-option label="上架" :value="1" />
          <el-option label="下架" :value="0" />
          <el-option label="待审核" :value="-1" />
          <el-option label="驳回" :value="-2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['supply:node:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['supply:node:edit']"
          >修改</el-button
        >
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['supply:node:remove']"-->
<!--          >删除</el-button-->
<!--        >-->
<!--      </el-col>-->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:base:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="baseList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="节点名称" align="center" prop="nodeName">
        <template #default="{ row }">
          <router-link :to="`/node/detail?id=${row.id}`">
            <el-button type="text">{{ row.nodeName }}</el-button>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="所属公司" align="center" prop="companyName" />
      <el-table-column label="节点类型" align="center" prop="nodeType">
        <template #default="{ row }">{{
          row.nodeType === 1 ? "章鱼" : "其他"
        }}</template>
      </el-table-column>
      <el-table-column label="节点Url" align="center" prop="nodeUrl" />
      <el-table-column label="节点联系人" align="center" prop="nodeLinkUser" />
      <el-table-column
        label="节点联系方式"
        align="center"
        prop="nodeLinkMobile"
      />

      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="120px">
        <template #default="{ row }">
          <span v-if="row.status === -1">未审核</span>
          <span v-else-if="row.status === -2">已驳回</span>
          <template v-else>
            <el-switch
              :value="row.status"
              :active-value="1"
              :inactive-value="0"
              @change="toggleStatus(row)"
            ></el-switch>
            <span>{{ row.status ? "上架" : "下架" }}</span>
          </template>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="260"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="mini"
            @click="openRecord(scope.row, 'search')"
            >下发记录</el-button
          >
          <el-button
            type="text"
            size="mini"
            @click="openRecord(scope.row, 'upload')"
            >上报记录</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['supply:node:edit']"
            >修改</el-button
          >
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="handleDelete(scope.row)"-->
          <!--            v-hasPermi="['supply:node:remove']"-->
          <!--            >删除</el-button-->
          <!--          >-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改基本信息对话框 -->
    <el-dialog :title="title"   :close-on-click-modal="false" :visible.sync="open" width="40vw" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="节点名称" prop="nodeName">
          <el-input v-model="form.nodeName" placeholder="请输入节点名称" />
        </el-form-item>
        <el-form-item label="所属供应商" prop="companyId">
          <el-select v-model="form.companyId" placeholder="请选择供应商">
            <el-option
              v-for="option in companyList"
              :key="option.id"
              :label="option.companyName"
              :value="option.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="节点类型" prop="nodeType">
          <el-radio-group v-model="form.nodeType">
            <el-radio :label="0">其他</el-radio>
            <el-radio :label="1">章鱼</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="节点Url" prop="nodeUrl">
          <el-input
            v-model="form.nodeUrl"
            placeholder="请输入节点Url"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系人" prop="nodeLinkUser">
          <el-input
            v-model="form.nodeLinkUser"
            placeholder="请输入联系人姓名"
            :maxlength="10"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系方式" prop="nodeLinkMobile">
          <el-input
            v-model="form.nodeLinkMobile"
            placeholder="请输入联系方式"
            :maxlength="11"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="登录账号" prop="nodeLoginUser">
          <el-input
            v-model="form.nodeLoginUser"
            placeholder="请输入登录账号"
            :maxlength="20"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="登录密码" prop="nodeLoginPwd">
          <el-input
            v-model="form.nodeLoginPwd"
            placeholder="请输入登录密码"
            :maxlength="20"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="地点" prop="nodeAddress">
          <el-input
            v-model="form.nodeAddress"
            placeholder="请输入地点"
            :maxlength="50"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            type="textarea"
            v-model="form.remark"
            placeholder="请输入备注"
            :maxlength="100"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="下发记录"
         :close-on-click-modal="false"
      :visible.sync="searchVisible"
      width="80vw"
      @close="closeRecordDialog"
    >
      <el-row :gutter="30" style="margin-bottom: 16px">
        <el-col :span="6">
          <el-input
            v-model="recordParams.keyword"
            placeholder="请输入关键词"
          ></el-input>
        </el-col>
        <el-col :span="4" :offset="14" style="text-align: right">
          <el-button type="primary" @click="reGetRecord">查询</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="recordLoading" :data="recordList">
        <el-table-column
          label="节点名称"
          prop="nodeName"
          align="center"
        ></el-table-column>
        <el-table-column label="任务类型" align="center" prop="taskType">
          <template #default="{ row }">
            {{ row.taskType === 1 ? "查询节点" : row.taskType === 6 ? "节点管理员信息同步" : row.taskType === 7 ? "订阅用户同步" : "未知"}}
          </template>
        </el-table-column>
        <el-table-column
          label="msgType"
          prop="msgType"
          align="center"
        ></el-table-column>
        <el-table-column label="任务执行状态" prop="taskStatus" align="center">
          <template #default="{ row }">{{
            taskStatusEnum[row.taskStatus]
          }}</template>
        </el-table-column>
        <el-table-column label="下发JSON数据" prop="jsonData" align="center">
          <template #default="{ row }">
            <el-button
              v-if="row.jsonData"
              type="primary"
              size="mini"
              @click="checkJsonDetail(row.jsonData)"
              >查看</el-button
            >
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column
          label="失败原因"
          prop="failReason"
          align="center"
        ></el-table-column>
        <el-table-column
          label="处理时间"
          prop="handleTime"
          width="160"
        ></el-table-column>
        <el-table-column label="查询耗时" prop="askSec" align="center">
          <template #default="{ row }">{{
            row.askSec ? `${row.askSec}秒` : "-"
          }}</template>
        </el-table-column>
        <el-table-column label="应答JSON内容" prop="msgReturn" align="center">
          <template #default="{ row }">
            <el-button
              v-if="row.msgReturn && row.msgReturn.jsonData"
              type="primary"
              size="mini"
              @click="checkJsonDetail(row.msgReturn.jsonData)"
              >查看</el-button
            >
            <span v-else>-</span>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="recordTotal > 0"
        :total="recordTotal"
        :page.sync="recordParams.pageNum"
        :limit.sync="recordParams.pageSize"
        @pagination="getRecordDetail"
      />
      <template #footer>
        <el-button type="primary" @click="searchVisible = false"
          >关闭</el-button
        >
      </template>
    </el-dialog>
    <el-dialog
      title="上报记录"
         :close-on-click-modal="false"
      :visible.sync="uploadVisible"
      width="80vw"
      @close="closeRecordDialog"
    >
      <el-row :gutter="30" style="margin-bottom: 16px">
        <el-col :span="6">
          <el-input
            v-model="recordParams.keyword"
            placeholder="请输入关键词"
          ></el-input>
        </el-col>
        <el-col :span="4" :offset="14" style="text-align: right">
          <el-button type="primary" @click="reGetRecord">查询</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="recordLoading" :data="recordList">
        <el-table-column
          label="报文编号"
          prop="sortNum"
          align="center"
        ></el-table-column>
        <el-table-column
          label="请求方法名"
          prop="requestMethod"
          align="center"
        ></el-table-column>
        <el-table-column label="请求JSON" prop="reqData" align="center">
          <template #default="{ row }">
            <el-button
              v-if="row.reqData"
              type="primary"
              size="mini"
              @click="checkJsonDetail(row.reqData)"
              >查看</el-button
            >
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column label="处理情况" prop="handleStatus" align="center">
          <template #default="{ row }">{{
            row.handleStatus === 1
              ? "已处理"
              : row.handleStatus === -1
              ? "失败"
              : row.handleStatus === -2
              ? "超时取消"
              : "待处理"
          }}</template>
        </el-table-column>
        <el-table-column
          label="处理时间"
          prop="handleTime"
          width="160"
          align="center"
        ></el-table-column>
        <el-table-column label="回复JSON" prop="respData" align="center">
          <template #default="{ row }">
            <el-button
              v-if="row.respData"
              type="primary"
              size="mini"
              @click="checkJsonDetail(row.respData)"
              >查看</el-button
            >
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          prop="createTime"
          align="center"
        ></el-table-column>
      </el-table>
      <pagination
        v-show="recordTotal > 0"
        :total="recordTotal"
        :page.sync="recordParams.pageNum"
        :limit.sync="recordParams.pageSize"
        @pagination="getRecordDetail"
      />
      <template #footer>
        <el-button type="primary" @click="uploadVisible = false">关闭</el-button>
      </template>
    </el-dialog>
    <el-dialog title="JSON内容" :visible.sync="jsonVisible">
      <div v-html="jsonData"></div>
    </el-dialog>
    <SuanliPopup :visible.sync="supplyVisible" :formData="resourceSupply" />
    <elementPopup :visible.sync="elementVisible" :formData="elementProvide" />
  </div>
</template>

<script>
import {
  getNodeList,
  createNode,
  updateNode,
  toggleNodeStatus,
  getNodeDetail,
} from "@/api/supply";
import {
  getCompanyDropdown,
} from "@/api/system/company";
import SuanliPopup from "@/views/manage/components/suanliPopup.vue";
import elementPopup from "@/views/element/elementProvide/components/editPopup.vue";
import recordMixin from "./mixin/record";

export default {
  name: "Base",
  dicts: ["info_type"],
  mixins: [recordMixin],
  components: { SuanliPopup, elementPopup },
  data() {
    return {
      companyList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 基本信息表格数据
      baseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        infoDetailDesc: null,
        infoFrom: null,
        infoTitle: null,
        infoType: null,
        noticeUserStatus: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        nodeName: [{ required: true, message: "请填写节点名称" }],
        companyId: [{ required: true, message: "请选择供应商" }],
        nodeType: [{ required: true, message: "请选择节点类型" }],
        nodeUrl: [{ required: true, message: "请输入节点Url" }],
        nodeLinkUser: [{ required: true, message: "请输入节点联系人" }],
        nodeLinkMobile: [{ required: true, message: "请输入节点联系方式" }],
        nodeLoginUser: [{ required: true, message: "请输入节点登录账号" }],
        nodeLoginPwd: [{ required: true, message: "请输入节点登录密码" }],
      },
      currRow: {},
      detailVisible: false,
    };
  },
  created() {
    this.getList();
    this.getCompanyList();
  },
  methods: {
    async getCompanyList() {
      const { data } = await getCompanyDropdown({
        status: 1,
        companyType: 1,
      });
      this.companyList = data;
    },
    async toggleStatus(row) {
      const { id, status } = row;
      const { code } = await toggleNodeStatus({
        id,
        status: Number(!status),
      });
      if (code === 200) {
        this.$message.success(status ? "下架成功" : "上架成功");
        this.$set(row, "status", Number(!status));
      }
    },
    checkNewsDetail(row) {
      this.currRow = row;
      this.detailVisible = true;
    },
    /** 查询基本信息列表 */
    getList() {
      this.loading = true;
      getNodeList(this.queryParams).then((response) => {
        this.baseList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        companyId: null,
        createBy: null,
        createTime: null,
        createUserId: null,
        isDelete: null,
        remark: null,
        updateBy: null,
        updateTime: null,
        updateUserId: null,
        infoDetailDesc: null,
        infoFrom: null,
        infoPicUrl: null,
        infoSimpleDesc: null,
        infoTitle: null,
        infoType: null,
        nodeId: null,
        noticeUserStatus: null,
        openUrl: null,
        dictType: null,
        status: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加节点信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getNodeDetail(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "添加节点信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateNode(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            createNode(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除基本信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBase(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/base/export",
        {
          ...this.queryParams,
        },
        `base_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
